package com.nbsaas.boot.example.rest.resource;

import com.nbsaas.boot.example.api.apis.ExampleCatalogApi;
import com.nbsaas.boot.example.data.entity.ExampleCatalog;
import com.nbsaas.boot.example.api.domain.request.ExampleCatalogRequest;
import com.nbsaas.boot.example.api.domain.response.ExampleCatalogResponse;
import com.nbsaas.boot.example.api.domain.simple.ExampleCatalogSimple;
import com.nbsaas.boot.example.rest.convert.ExampleCatalogSimpleConvert;
import com.nbsaas.boot.example.rest.convert.ExampleCatalogEntityConvert;
import com.nbsaas.boot.example.rest.convert.ExampleCatalogResponseConvert;
import com.nbsaas.boot.example.data.repository.ExampleCatalogRepository;

import java.io.Serializable;
import com.nbsaas.boot.jpa.data.core.BaseResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;

import java.util.function.Function;
import com.nbsaas.boot.rest.request.PageRequest;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.example.api.domain.request.ExampleCatalogSearch;
import com.nbsaas.boot.rest.utils.TreeUtils;
import java.util.List;
/**
*   案例分类-业务接口实现
*/
@Transactional
@Service
public class ExampleCatalogResource extends BaseResource<ExampleCatalog,ExampleCatalogResponse, ExampleCatalogSimple, ExampleCatalogRequest>  implements ExampleCatalogApi {

    @Resource
    private ExampleCatalogRepository exampleCatalogRepository;

    @Override
    public JpaRepositoryImplementation<ExampleCatalog, Serializable> getJpaRepository() {
        return exampleCatalogRepository;
    }

    @Override
    public Function<ExampleCatalog, ExampleCatalogSimple> getConvertSimple() {
        return new ExampleCatalogSimpleConvert();
    }

    @Override
    public Function<ExampleCatalogRequest, ExampleCatalog> getConvertForm() {
        return new ExampleCatalogEntityConvert();
    }

    @Override
    public Function<ExampleCatalog, ExampleCatalogResponse> getConvertResponse() {
        return new ExampleCatalogResponseConvert();
    }



    @Transactional(readOnly = true)
    @Override
    public ListResponse<ExampleCatalogSimple> list(PageRequest request) {
        ExampleCatalogSimpleConvert convert=new ExampleCatalogSimpleConvert();
        if (request instanceof ExampleCatalogSearch){
             ExampleCatalogSearch searchRequest=(ExampleCatalogSearch)request;
             convert.setFetch(searchRequest.getFetch());
        }
        return listSimple(request,convert);
    }


    @Transactional(readOnly = true)
    @Override
    public ListResponse<ExampleCatalogSimple> root(ExampleCatalogSearch search) {
        ListResponse<ExampleCatalogSimple> result=new  ListResponse<>();
        List<ExampleCatalogSimple> simples = list(search).getData();
        result.setData(TreeUtils.tree(simples));
        return result;
    }


}


